using System;
using Microsoft.AspNetCore.Mvc;
using ApiProject.Api.Entity;
using System.Linq;
using ApiProject.Api.Repository;
using Microsoft.AspNetCore.Hosting;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using ApiProject.Api.PramaModel;


// 轮播图管理
namespace ApiProject.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CarouselpicturesController : ControllerBase
    {
        //轮播图


        private IRepository<CarouselPictures> _carousrelRespository;

        private readonly IWebHostEnvironment _hostingEnvironment;

        public CarouselpicturesController(IWebHostEnvironment hostingEnvironment, IRepository<CarouselPictures> carousrelRespository)
        {
            _hostingEnvironment = hostingEnvironment;
            _carousrelRespository = carousrelRespository;

        }


        //添加轮播图
        //Post /carouselpictures
        [HttpPost]
        public dynamic AddPicture()
        {
            var files = HttpContext.Request.Form.Files;

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            Console.WriteLine(showfilePath);

            var newpic = new CarouselPictures
            {
                CarouselPicturePath = showfilePath
            };

            _carousrelRespository.Insert(newpic);

            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Data = newpic,
                Msg = "添加图片成功"
            });
        }

        //获取所有的轮播图
        //Get /carouselpictures/cps
        [HttpGet,Route("cps")]
        public dynamic GetPicture()
        {
            var allPic = _carousrelRespository.Table.Where(x => x.IsDelete == false).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = allPic
            });
        }

        //是否启用禁用轮播图
        //Post /carouselpictures/isenablepic/${id}
        [HttpPost, Route("isenablepic/{id}")]
        public dynamic IsEnable(int id, NewQrCode model)
        {
            var pictures = _carousrelRespository.GetById(id);

            var num = model.IsEnable;

            if (pictures != null)
            {
                if (num == false)
                {
                    pictures.IsEnable = false;
                    pictures.UpdatedTime = DateTime.Now;
                    _carousrelRespository.Update(pictures);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = pictures,
                        Msg = string.Format("已禁用", id)
                    });
                }
                else
                {
                    pictures.IsEnable = true;
                    pictures.UpdatedTime = DateTime.Now;
                    _carousrelRespository.Update(pictures);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = pictures,
                        Msg = string.Format("已启用", id)
                    });
                }

            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "禁用失败" });
            }
        }

    
        //是否删除轮播图
        //Post /carouselpictures/deletepic/${id}
        [HttpPost, Route("deletepic/{id}")]
        public dynamic DeleteCode(int id)
        {

            var pictures = _carousrelRespository.GetById(id);

            pictures.IsDelete = true;
            pictures.UpdatedTime = DateTime.Now;

            _carousrelRespository.Update(pictures);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = pictures,
                Msg = "删除二维码成功"
            });
        }
    
    
        //编辑轮播图
        //Put /carouselpictures/editcar/${id}
        [HttpPut,Route("editcar/{id}")]
        public dynamic EditCarPicture(int id){
             var files = HttpContext.Request.Form.Files;

            var newpic = _carousrelRespository.GetById(id);

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            newpic.CarouselPicturePath = showfilePath;
            newpic.UpdatedTime = DateTime.Now;

            _carousrelRespository.Update(newpic);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = newpic
            });

        }


        //获得所有轮播图
        //get /carouselpictures/carousel
        [HttpGet,Route("carousel")]
        public dynamic GetCarPicture()
        {
            var allPic = _carousrelRespository.Table.Where(x => x.IsDelete == false && x.IsEnable == true).ToList();

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = allPic
            });
        }




    }
}